package org.itboys.mobile.controller.weixin.account;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.entity.mongo.account.SundayAccount;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.util.HttpRequestUtil;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.mobile.service.mongo.weixin.account.SundayWeixinAccountService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created on 2017/11/29.
 * @author 朱坤涛
 */
@Controller
@RequestMapping("/sunday/weixin/account")
public class SundayWeixinAccountController extends BaseController {
    @Autowired
    private SundayWeixinAccountService accountService;
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(
            @ApiParam(required = true, name = "accountId", value = "账户Id,新增时传入0") @RequestParam(value = "accountId", required = false) Long accountId,
            @ApiParam(required = true, name = "type", value = "账户类型(1-银行卡 2-支付宝 3-微信)")@RequestParam(value = "type", required = true) Integer type,
            @ApiParam(required = true, name = "account", value = "账户号") @RequestParam(value = "account", required = true) String account,
            @ApiParam(required = true, name = "name", value = "账户名") @RequestParam(value = "name", required = true) String name,
            @ApiParam(required = true, name = "mobile", value = "填写银行卡预留手机号")@RequestParam(value = "mobile",required = true)String mobile,
            @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
            @ApiParam(required = false, name = "bank", value = "开户银行") @RequestParam(value = "bank", required = false) String bank,
            HttpServletRequest request, HttpServletResponse response) {
        try {
            Long memberId = HttpRequestUtil.getUser(request).getId();
            SundayAccount sundayAccount = new SundayAccount();
            if (type==2){
                sundayAccount.setBankName("支付宝");
            }else if (type==3){
                sundayAccount.setBankName("微信");
            }else {
                sundayAccount.setBankName(bank);
            }
            bank = sundayAccount.getBankName();
            accountService.saveAccouunt(accountId,memberId,name,bank,account,type,mobile,code);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJson(response, ResultPageDOHelper.getErrorResultDO(-1, "系统异常!"));
        }
    }

    //获取账户列表
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public void list(HttpServletRequest request,
                     HttpServletResponse response){
        try {
            Long memberId = HttpRequestUtil.getUser(request).getId();
            Map<String,Object> param = new HashMap<>();
            param.put("memberId",memberId);
            PageResult<SundayAccount> result= accountService.pageQuery(request,param);
            ResponseMessage.success(result.getData());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //设置默认账户
    @ApiOperation(value = "设置默认账户")
    public void serDefault(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "accountId",required = true)Long accountId){
        try {
            Long memberId = HttpRequestUtil.getUser(request).getId();
            Map<String,Object> param = new HashMap<>();
            accountService.setDefaultAccount(accountId,memberId);
            ResponseMessage.success();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //获取默认账户
    @ApiOperation(value = "获取默认账户")
    @RequestMapping(value = "/getDefault",method = RequestMethod.POST)
    public void getDefault(
            HttpServletRequest request,
            HttpServletResponse response){
        try {
            Long memberId = HttpRequestUtil.getUser(request).getId();;
            SundayAccount account = accountService.getDefaultAccount(memberId);
            ResponseMessage.success(account);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //删除账户
    @ApiOperation(value = "删除默认账户")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "accountId",required = true)Long accountId){
        try {
            accountService.deleteAccount(accountId);
            ResponseMessage.success();
        }catch (Exception e){
            e.printStackTrace();

        }
    }
}
